﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deeper.Common
{
	/// <summary>
	/// 验证类……
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class Validator<T> where T : new()
	{
		private T Entity { set; get; }
		public bool IsError { get { return ErrorMessage.Count > 0; } }
		public IList<string> ErrorMessage { private set; get; }

		public string ErrorMessageString
		{
			get
			{
				string str = string.Empty;
				ErrorMessage.ToList().ForEach(s => str += s + "\n");
				return str;
			}
		}
		public Validator(T entity)
		{
			Entity = entity;
			ErrorMessage = new List<string>();
		}
		/// <summary>
		/// 根据表达式验证，如果表达式不成立，就添加错误信息
		/// </summary>
		/// <param name="predicate">表达式</param>
		/// <param name="errorMessage">错误信息</param>
		/// <returns></returns>
		public Validator<T> Validate(Predicate<T> predicate, string errorMessage)
		{
			if (!predicate(this.Entity)) ErrorMessage.Add(errorMessage);
			return this;
		}


	}


}
